//
// Created by Administrator on 2021/9/2.
//

#ifndef OPENGLLEARN_OBJECT_H
#define OPENGLLEARN_OBJECT_H

#include "Sbm.h"
#include "glad/glad.h"

namespace SBM
{
    class Object
    {
    public:
        Object() = default;
        ~Object() = default;

        void Load(const char* filename);
        void Free();

        void Render(unsigned int instanceCount = 1, unsigned int baseInstance = 0)
        {
            RenderSubObject(0, instanceCount, baseInstance);
        }
        void RenderSubObject(unsigned int objectIndex, unsigned int instanceCount = 1, unsigned int baseInstance = 0);
        void GetSubObjectInfo(unsigned int index, GLuint& first, GLuint& count)
        {
            if(index >= numSubObjects_)
            {
                first = 0;
                count = 0;
            }
            else
            {
                first = subObjects_[index].first;
                count = subObjects_[index].count;
            }
        }

        unsigned int numSubObjects() const { return numSubObjects_; }
        GLuint vao() const { return vao_; }
    private:
        GLuint dataBuffer_;
        GLuint vao_;
        GLuint indexType_;
        GLuint indexOffset_;

        static const unsigned int MAX_SUB_OBJECTS = 256;

        unsigned int numSubObjects_;
        SBM_SUB_OBJECT_DECL subObjects_[MAX_SUB_OBJECTS]{};
    };
}

#endif //OPENGLLEARN_OBJECT_H
